import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class DESEncryption {

    public static String encryptDES(String plainText, String hexKey, String hexIv) throws Exception {
        // 将16进制字符串转换为字节数组
        byte[] keyBytes = hexStringToByteArray(hexKey);
        byte[] ivBytes = hexStringToByteArray(hexIv);
        
        // 创建密钥和IV
        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "DES");
        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        
        // 初始化加密器 (DES/CBC/PKCS5Padding)
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
        
        // 加密数据
        byte[] encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
        
        // 返回Base64编码的加密结果
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String encryptAES(String plainText, String key, String iv) throws Exception {
        // 检查密钥长度（AES-128:16字节，AES-192:24字节，AES-256:32字节）
        if (key.length() != 16 && key.length() != 24 && key.length() != 32) {
            throw new IllegalArgumentException("Key长度必须是16(AES-128)、24(AES-192)或32(AES-256)字节");
        }

        // 检查IV长度（必须是16字节）
        if (iv.length() != 16) {
            throw new IllegalArgumentException("IV长度必须是16字节");
        }

        // 使用UTF-8编码获取字节数组
        byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
        byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8);

        // 创建密钥和IV
        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

        // 初始化加密器 (AES/CBC/PKCS5Padding)
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);

        // 加密数据
        byte[] encrypted = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));

        // 返回Base64编码的加密结果
        return Base64.getEncoder().encodeToString(encrypted);
    }

    // 16进制字符串转字节数组
    private static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }

    public static void main(String[] args) throws Exception {
        String plainText = "Hj19981011.";

        String key = "575F023D1EA9414CDC3CFCC99E78F91A";
        String hexKey = key.substring(0,16);  // 8字节(16个十六进制字符)的DES密钥
        String hexIv =  key.substring(16);   // 8字节的初始化向量

        System.out.println("hexKey:"+hexKey+" hexIv:"+hexIv);

//        String encrypted = encryptDES(plainText, hexKey, hexIv);
        String encrypted = encryptAES(plainText, hexKey, hexIv);

        // 期望结果
        String result = "P7rovLLZNB9QQMXzRiUP2A==";
        System.out.println("Encrypted: 䐵匠䴵" + encrypted);
        System.out.println("状态: "+result.equals(encrypted));
    }


}